<%-- 
    Document   : document
    Created on : 2013-5-12, 21:16:09
    Author     : piaohao
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%= basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="../main/common.jsp" %>
        <title>文件管理</title>
        <script>
            Ext.onReady(function() {
                var documentForm = Ext.create("Ext.form.Panel", {
                    url: "doc/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 3
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "名称", name: "document.name", allowBlank: false},
                        {
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "文件来源",
                            name: "document.from",
                            displayField: 'text',
                            valueField: 'value',
                            editable: false,
                            store: {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "行业管理文件", value: "行业管理文件"},
                                    {text: "上级文件", value: "上级文件"},
                                    {text: "本单位文件", value: "本单位文件"}
                                ]
                            }
                        },
                        {
                            xtype: "combo",
                            fieldLabel: '文件类型',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "情况简报", value: "情况简报"},
                                    {text: "普通文件", value: "普通文件"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "document.type",
                            value: ""
                        },
                        {fieldLabel: "文号", name: "document.number"},
                        {
                            xtype: "datefield",
                            fieldLabel: "发布日期", 
                            editable: false,
                            name: "publish_date",
                            format:"Y-m-d"
                        },
                        {
                            id: "orgCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属机构",
                            name: "document.org_id",
                            displayField: 'name',
                            valueField: 'id',
                            editable: false,
                            store: {
                                fields: ['name', 'id'],
                                proxy: {
                                    type: 'ajax',
                                    url: 'org/getAll',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            }
                        },
                        {xtype: "htmleditor", fieldLabel: "文件内容", name: "document.content", colspan: 3, width: 1000, height: 300},
                        {
                            xtype: 'checkboxgroup',
                            fieldLabel: '接收机构',
//                            defaultType: 'checkboxfield',
                            colspan: 3,
                            width: 1000,
                            items: [
            <c:forEach var="org" items="${orgs}" varStatus="tag">
                <c:if test="${tag.last}">
                    {
                                    boxLabel: '${org.name}',
                                    name: 'to',
                                    inputValue: '${org.id}'
                                }
                </c:if>
                <c:if test="${!tag.last}">
                    {
                                    boxLabel: '${org.name}',
                                    name: 'to',
                                    inputValue: '${org.id}'
                                }, 
                </c:if>
                                           </c:forEach>

                                                    ]
                                                },
                                                {name: "document.id", xtype: "hiddenfield"}
                                            ],
                                            buttons: [{
                                                    text: '重置',
                                                    iconCls: "refresh",
                                                    handler: function() {
                                                        this.up('form').getForm().reset();
                                                    }
                                                }, {
                                                    text: '提交',
                                                    formBind: true, //only enabled once the form is valid
                                                    disabled: true,
                                                    iconCls: "save",
                                                    handler: function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            form.submit({success: function(form, action) {
                                                                    var json = action.result;
                                                                    Ext.Msg.alert('提示', json.msg, function() {
                                                                        documentWindow.close();
                                                                        documentStore.load();
                                                                    });
                                                                },
                                                                failure: function(form, action) {
                                                                    Ext.Msg.alert('提示', action.result.msg);
                                                                }
                                                            });
                                                        }
                                                    }
                                                }, {
                                                    text: "关闭",
                                                    iconCls: "close",
                                                    handler: function() {
                                                        documentWindow.close();
                                                    }
                                                }]
                                        });
                                        var documentWindow = Ext.create("Ext.window.Window", {
                                            title: "文件信息", maximized: true,
                                            maximizable: true,
                                            closeAction: "hide",
//                    layout: "fit",
                                            overflowY: "auto",
                                            items: [documentForm]
                                        });
                                        var documentStore = Ext.create('Ext.data.Store', {
                                            fields: ["id", "name", "number", "publish_date", "from", "type","org_id"],
                                            pageSize: 15,
                                            proxy: {
                                                type: 'ajax',
                                                url: 'doc/list',
                                                reader: {
                                                    type: 'json',
                                                    root: 'data'
                                                }
                                            },
                                            autoLoad: true
                                        });
                                        var documentGrid = Ext.create("Ext.grid.Panel", {
                                            title: "文件列表",
                                            columnLines: true,
                                            frame: true,
                                            selType: 'checkboxmodel', // 设置选择模式 
                                            multiSelect: true, // 允许多选
                                            forceFit: true,
                                            columns: [
                                                {header: '主键', dataIndex: 'id', hidden: true},
                                                {header: '名称', dataIndex: 'name', width: 120},
                                                {header: '文号', dataIndex: 'number', width: 100},
                                                {header: '文件类型', dataIndex: 'type', width: 100},
                                                {header: '文件来源', dataIndex: 'from', width: 100},
                                                {header: '发布日期', dataIndex: 'publish_date', width: 100}
                                            ],
                                            store: documentStore,
                                            tbar: [
                                                {xtype: "button", text: "查看", iconCls: "search",
                                                    handler: function() {
                                                        var sel = documentGrid.getSelectionModel().getSelection();
                                                        if (sel.length !== 1) {
                                                            Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                                            return;
                                                        }
                                                        var record = sel[0].data;
                                                        var detailWindow = Ext.create("Ext.window.Window", {
                                                            title: "文件信息",
                                                            width: 800,
                                                            height: 400,
                                                            maximizable:true,
                                                            modal:true,
                                                            closeAction: "hide",
                                                            layout: "fit",
                                                            html: "<iframe id='detailFrame' src='doc/showDetail/" + record.id + "' width='100%' height='100%'></iframe>",
                                                            buttons: [
                                                                {text: "打印", iconCls: "print",
                                                                    handler: function() {
                                                                        Ext.get("detailFrame").dom.contentWindow.print();
                                                                    }},
                                                                {text: "关闭", iconCls: "close",
                                                                    handler: function() {
                                                                        detailWindow.close();
                                                                    }}
                                                            ]
                                                        });
                                                        detailWindow.show();
                                                    }},
                                                {xtype: 'tbseparator'},
                                                {xtype: "button", text: "登记", iconCls: "add",
                                                    handler: function() {
                                                        documentForm.getForm().reset();
                                                        documentForm.getForm().url = "doc/add";
                                                        documentWindow.show();
                                                    }},
                                                {xtype: 'tbseparator'},
                                                {xtype: "button", text: "变更", iconCls: "edit",
                                                    handler: function() {
                                                        var sel = documentGrid.getSelectionModel().getSelection();
                                                        if (sel.length !== 1) {
                                                            Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                                            return;
                                                        }
                                                        documentForm.getForm().reset();
                                                        var record = sel[0];
                                                        documentForm.getForm().url = "doc/update";
                                                        var tag = true;
                                                        Ext.Ajax.request({
                                                            url: "doc/getDocById",
                                                            params: {
                                                                docId: record.get("id")
                                                            },
                                                            success: function(response) {
                                                                var json = Ext.JSON.decode(response.responseText);
                                                                documentForm.getForm().setValues({
                                                                    "document.content": json.content
                                                                });
                                                            },
                                                            failure: function(response) {
                                                                var json = Ext.JSON.decode(response.responseText);
                                                                Ext.Msg.alert("提示", json.msg, function() {
                                                                    tag = false;
                                                                });
                                                            }
                                                        });
                                                        if (!tag) {
                                                            Ext.Msg.alert("提示", "获取文档内容失败！");
                                                            return;
                                                        }
                                                        documentWindow.show();
                                                        documentForm.getForm().setValues({
                                                            "document.id": record.get("id"),
                                                            "document.name": record.get("name"),
                                                            "document.number": record.get("number"),
                                                            "document.type": record.get("type"),
                                                            "document.from": record.get("from"),
                                                            "publish_date": record.get("publish_date").substring(0,10),
                                                            "document.org_id":record.get("org_id")
                                                        });
                                                        Ext.getCmp("orgCombo").getStore().load();
                                                    }
                                                },
                                                {xtype: 'tbseparator'},
                                                {xtype: "button", text: "删除", iconCls: "remove",
                                                    handler: function() {
                                                        Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                                            if (id === "yes") {
                                                                var mask = new Ext.LoadMask(documentGrid, {msg: "正在删除..."});
                                                                mask.show();
                                                                var sel = documentGrid.getSelectionModel().getSelection();
                                                                var ids = [];
                                                                for (i = 0; i < sel.length; i++) {
                                                                    ids.push(sel[i].data.id);
                                                                }
                                                                Ext.Ajax.request({
                                                                    url: "doc/delete",
                                                                    params: {
                                                                        ids: ids
                                                                    },
                                                                    success: function(response) {
                                                                        var json = Ext.JSON.decode(response.responseText);
                                                                        Ext.Msg.alert("提示", json.msg, function() {
                                                                            documentGrid.getStore().remove(sel);
                                                                            mask.hide();
                                                                        });
                                                                    },
                                                                    failure: function(response) {
                                                                        var json = Ext.JSON.decode(response.responseText);
                                                                        Ext.Msg.alert("提示", json.msg, function() {
                                                                            mask.hide();
                                                                        });
                                                                    }
                                                                });
                                                            }
                                                        });
                                                    }
                                                }
                                            ],
                                            bbar: Ext.create('Ext.PagingToolbar', {
                                                store: documentStore,
                                                displayInfo: true
                                            })
                                        });
                                        var docAccStore=Ext.create('Ext.data.Store', {
                                            fields: ["id", "name", "status", "from", "org_name"],
                                            pageSize: 15,
                                            proxy: {
                                                type: 'ajax',
                                                url: 'doc/listAcc',
                                                reader: {
                                                    type: 'json',
                                                    root: 'data'
                                                }
                                            },
                                            autoLoad: true
                                        });
                                        var docAccGrid = Ext.create("Ext.grid.Panel", {
                                            title: "文件列表",
                                            columnLines: true,
                                            frame: true,
                                            selType: 'checkboxmodel', // 设置选择模式 
                                            multiSelect: true, // 允许多选
                                            forceFit: true,
                                            columns: [
                                                {header: '主键', dataIndex: 'id', hidden: true},
                                                {header: '名称', dataIndex: 'name', width: 120},
                                                {header: '状态', dataIndex: 'status', width: 100,renderer:function(value){
                                                    if(value==="未接收"){
                                                        return "<span style='color:red'>"+value+"</span>";
                                                    }else{
                                                        return "<span style='color:blue'>"+value+"</span>";
                                                    }
                                                }
                                                },
                                                {header: '发布机构', dataIndex: 'from', width: 100},
                                                {header: '所属机构', dataIndex: 'org_name', width: 100}
                                            ],
                                            store: docAccStore,
                                            tbar: [
                                                {xtype: "button", text: "查看", iconCls: "search",
                                                    handler: function() {
                                                        var sel = docAccGrid.getSelectionModel().getSelection();
                                                        if (sel.length !== 1) {
                                                            Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                                            return;
                                                        }
                                                        var record = sel[0].data;
                                                        var detailWindow = Ext.create("Ext.window.Window", {
                                                            title: "文件信息",
                                                            width: 800,
                                                            height: 400,
                                                            maximizable:true,
                                                            modal:true,
                                                            closeAction: "hide",
                                                            layout: "fit",
                                                            html: "<iframe id='detailFrame' src='doc/showAccDetail/" + record.id + "' width='100%' height='100%'></iframe>",
                                                            buttons: [
                                                                {text: "打印", iconCls: "print",
                                                                    handler: function() {
                                                                        Ext.get("detailFrame").dom.contentWindow.print();
                                                                    }},
                                                                {text: "关闭", iconCls: "close",
                                                                    handler: function() {
                                                                        detailWindow.close();
                                                                    }}
                                                            ]
                                                        });
                                                        detailWindow.show();
                                                    }},
                                                {xtype: 'tbseparator'},
                                                {xtype: "button", text: "接收", iconCls: "add",
                                                    handler: function() {
                                                    var mask = new Ext.LoadMask(docAccGrid, {msg: "正在处理..."});
                                                                mask.show();
                                                    var sel = docAccGrid.getSelectionModel().getSelection();
                                                        if (sel.length !== 1) {
                                                            Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                                            mask.hide();
                                                            return;
                                                        }
                                                        var record = sel[0];
                                                        if(record.get("status").indexOf("已接收")!==-1){
                                                            Ext.Msg.alert("提示","此文件已接收");
                                                            mask.hide();
                                                            return ;
                                                        }    
                                                        Ext.Ajax.request({
                                                                    url: "doc/accept",
                                                                    params: {
                                                                        id: record.get("id")
                                                                    },
                                                                    success: function(response) {
                                                                        var json = Ext.JSON.decode(response.responseText);
                                                                        Ext.Msg.alert("提示", json.msg, function() {
                                                                            docAccGrid.getStore().load();
                                                                            mask.hide();
                                                                        });
                                                                    },
                                                                    failure: function(response) {
                                                                        var json = Ext.JSON.decode(response.responseText);
                                                                        Ext.Msg.alert("提示", json.msg, function() {
                                                                            mask.hide();
                                                                        });
                                                                    }
                                                                });
                                                    }},
                                                {xtype: 'tbseparator'},
                                                {xtype: "button", text: "删除", iconCls: "remove",
                                                    handler: function() {
                                                        Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                                            if (id === "yes") {
                                                                var mask = new Ext.LoadMask(docAccGrid, {msg: "正在删除..."});
                                                                mask.show();
                                                                var sel = docAccGrid.getSelectionModel().getSelection();
                                                                var ids = [];
                                                                for (i = 0; i < sel.length; i++) {
                                                                    ids.push(sel[i].data.id);
                                                                }
                                                                Ext.Ajax.request({
                                                                    url: "doc/deleteAcc",
                                                                    params: {
                                                                        ids: ids
                                                                    },
                                                                    success: function(response) {
                                                                        var json = Ext.JSON.decode(response.responseText);
                                                                        Ext.Msg.alert("提示", json.msg, function() {
                                                                            docAccGrid.getStore().remove(sel);
                                                                            mask.hide();
                                                                        });
                                                                    },
                                                                    failure: function(response) {
                                                                        var json = Ext.JSON.decode(response.responseText);
                                                                        Ext.Msg.alert("提示", json.msg, function() {
                                                                            mask.hide();
                                                                        });
                                                                    }
                                                                });
                                                            }
                                                        });
                                                    }
                                                }
                                            ],
                                            bbar: Ext.create('Ext.PagingToolbar', {
                                                store: docAccStore,
                                                displayInfo: true
                                            })
                                        });
                                        var viewport = Ext.create("Ext.Viewport", {
                                            layout: {type: "border"},
                                            items: [
                                                {
                                                id:"mainTab",
                                                region: "center", 
                                                layout: "fit",xtype:"tabpanel",
                                                items: [{
                                                    title:"发布文件",
                                                    layout:"fit",
                                                    items:[documentGrid]
                                                    },{
                                                    title:"接收文件",
                                                    layout:"fit",
                                                    items:[docAccGrid]
                                                    }
                                                    ],
                                                listeners:{
                                                    render:function(){
                                                        Ext.getCmp("mainTab").setActiveTab(${tab});
                                                    }
                                                }
                                                }
                                            ]
                                        });
                                    });
        </script>
    </head>
    <body>
    </body>
</html>
